package com.itboye.mengwu.luntan.comment.adapter;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Bitmap.Config;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

import com.itboye.config.GlobalConfig;
import com.itboye.core.utils.XImageLoader;
import com.itboye.inageview.ImagePagerActivity;
import com.itboye.inageview.NoScrollGridAdapter;
import com.itboye.inageview.NoScrollGridView;
import com.itboye.mengwu.R;
import com.itboye.mengwu.data.model.TieZiBaoHanReply;
import com.itboye.mengwu.data.model.TieZiBaoHanReply.GetAList;
import com.itboye.mengwu.data.model.TieZiBaoHanReply.GetAList.ReplyInfo;
import com.itboye.mengwu.data.model.TieZiBaoHanReplyBaoBaoLe;
import com.itboye.mengwu.data.model.TieZiModel;
import com.itboye.mengwu.luntan.comment.bean.Comment;
import com.itboye.mengwu.luntan.comment.view.MyListView;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

public class DetailCommentAdapter extends BaseAdapter {
	private LayoutInflater inflater;
	private ViewHolder viewHolder;
	private Context context;
	private OnClickListener replyToCommentListener;
	private OnClickListener replyToReplyListener;
	private TieZiBaoHanReplyBaoBaoLe.GetAList commentList;

	public DetailCommentAdapter(Context context, TieZiBaoHanReplyBaoBaoLe.GetAList  commentList,
			OnClickListener replyToCommentListener,
			CommentReplyAdapter myAdapter, OnClickListener replyToReplyListener) {
		this.context = context;
		this.inflater = LayoutInflater.from(context);
//		this.commentList = new ArrayList<TieZiBaoHanReply.GetAList>();
		this.commentList=commentList;
		this.replyToCommentListener = replyToCommentListener;
		this.replyToReplyListener = replyToReplyListener;
	}

	public void clearList() {
//		this.commentList.get;
	}

//	public void updateList(GetAList commentList) {
//		this.commentList.getReplyInfo().getList().addAll(commentList);
//	}

	@Override
	public int getCount() {
		return commentList.getReplyInfo().getList().size();
	}

	@Override
	public Object getItem(int position) {
		return  commentList.getReplyInfo();
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.item_list, null);
			viewHolder = new ViewHolder();
//			viewHolder.iv_user_photo = (ImageView) convertView
//					.findViewById(R.id.iv_user_photo);
			viewHolder.tv_user_name = (TextView) convertView
					.findViewById(R.id.tv_title);
			viewHolder.tv_user_comment = (TextView) convertView
					.findViewById(R.id.tv_content);
			viewHolder.iv_avatar = (ImageView) convertView.findViewById(R.id.iv_avatar);
			viewHolder.btn_comment_reply = (TextView) convertView
					.findViewById(R.id.tetPing);
			viewHolder.txt_time=(TextView) convertView.findViewById(R.id.textTime);
			viewHolder.lv_user_comment_replys = (MyListView) convertView
					.findViewById(R.id.lv_user_comment_replys);
			viewHolder.tetzan=(TextView) convertView.findViewById(R.id.tetzan);
			viewHolder.imgZan=(ImageView) convertView.findViewById(R.id.imgZan);
			viewHolder.gridview = (NoScrollGridView) convertView.findViewById(R.id.gridview);
			viewHolder.btn_comment_reply.setTag(position);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
//		TieZiBaoHanReply.GetAList comment = getItem(position);
		viewHolder.tv_user_name.setText(commentList.getReplyInfo().getList().get(position).getAuthor());
		viewHolder.tv_user_comment.setText(commentList.getReplyInfo().getList().get(position).getComment());
		// 设置评论列表的点击效果透明
		viewHolder.lv_user_comment_replys.setSelector(new ColorDrawable(
				Color.TRANSPARENT));
		viewHolder.txt_time.setText(new SimpleDateFormat("yyyy-MM-dd").format(Long.parseLong(commentList.getReplyInfo().getList().get(position).getDateline())*1000));
//		if(Integer.parseInt(commentList.getReplyInfo().getList().get(position).getIsLike())==1){
//			viewHolder.imgZan.setImageResource(R.drawable.su);
//		}else{
//			viewHolder.imgZan.setImageResource(R.drawable.se);
//		}
		//判断当前评论是否有回复
		if (commentList.getReplyInfo().getList()!=null) {
			viewHolder.lv_user_comment_replys
					.setAdapter(new CommentReplyAdapter(context, commentList
							.getReplyInfo().getList(), position,
							replyToReplyListener));
		}
		viewHolder.tetzan.setTag(position);
		//记录点击回复按钮时对应的position,用于确定所回复的对象
		viewHolder.btn_comment_reply.setTag(position);
		viewHolder.btn_comment_reply.setOnClickListener(replyToCommentListener);
		final ArrayList<String> arrayImage=commentList.getImg();
		if (arrayImage == null || arrayImage.size() == 0) { // 没有图片资源就隐藏GridView
			viewHolder.gridview.setVisibility(View.GONE);
		} else {
			viewHolder.gridview.setAdapter(new NoScrollGridAdapter(context,  arrayImage));
		}
		// 使用ImageLoader加载网络图片
			XImageLoader.load(GlobalConfig.API_URL+"/picture/index?id="+commentList.getReplyInfo().getList().get(position).getHead(), viewHolder.iv_avatar);
				// 点击回帖九宫格，查看大图
				viewHolder.gridview.setOnItemClickListener(new OnItemClickListener() {

					@Override
					public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
						// TODO Auto-generated method stub
						imageBrower(position, arrayImage);
					}
				});
		return convertView;
	}
	

	/**
	 * 打开图片查看器
	 * 
	 * @param position
	 * @param urls2
	 */
	protected void imageBrower(int position, ArrayList<String> urls2) {
		Intent intent = new Intent(context, ImagePagerActivity.class);
		// 图片url,为了演示这里使用常量，一般从数据库中或网络中获取
		intent.putExtra(ImagePagerActivity.EXTRA_IMAGE_URLS, urls2);
		intent.putExtra(ImagePagerActivity.EXTRA_IMAGE_INDEX, position);
		context.startActivity(intent);
	}

	public class ViewHolder {
		private ImageView iv_avatar,imgZan;
		private TextView txt_time;
		private ImageView iv_user_photo; // 评论者 头像
		private TextView tv_user_name; // 评论者 昵称
		private TextView tv_user_comment; // 评论者 一级品论内容
		// private TextView tv_user_comment_date; //
		private TextView btn_comment_reply; // 评论者 二级评论按钮
		private MyListView lv_user_comment_replys; // 评论者 二级品论内容列表
		private NoScrollGridView gridview;
		TextView tetzan;
	}
}
